package com.happyass.controller;


import com.happyass.pojo.Booking;
import com.happyass.pojo.Ticket;
import com.happyass.service.BookinfoService;
import com.happyass.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/ticket")
public class TticketController {

    @Autowired
    TicketService ticketService;

    //访问购票主页面
    @RequestMapping("/list")
    public String list(Model map, Ticket ticket, int page){
        //查询所有
        HashMap<String,Object> list = ticketService.findAll(ticket,page);
        map.addAttribute("ticketsList",list.get("ticketsList"));
        //设置当前页
        map.addAttribute("nowPage",list.get("nowPage"));
        //获得前一页
        map.addAttribute("lastPage",list.get("lastPage"));
        //获得下一页
        map.addAttribute("nextPage",list.get("nextPage"));
        //获得总页码
        map.addAttribute("conPage",list.get("conPage"));
        //获得货物总数
        map.addAttribute("conEle",list.get("conEle"));
        map.addAttribute("ticketId",ticket.getTicketId());
        map.addAttribute("ticketName",ticket.getTicketName());
        map.addAttribute("ticketPrice",ticket.getTicketPrice());
        return "ticket-list";
    }

    //删除票信息
    @RequestMapping("/delTicket")
    @ResponseBody
    public HashMap<String,Object> delTicket(int ticketId) {
        HashMap<String,Object> map = new HashMap<String,Object>();
        ticketService.delDataTicket(ticketId);
        map.put("info","删除成功");
        return map;
    }

    //转到新增票界面
    @RequestMapping("/toAddTicket")
    public String toAddTicket(Model map){
        return "ticket-Add";
    }

    //新增票
    @RequestMapping("/addTicket")
    @ResponseBody
    public HashMap<String,Object> addTicket(Ticket ticket,HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        ticketService.addDataTicket(ticket);
        map.put("info","添加成功");
        return map;
    }

    //转到修改货物界面
    @RequestMapping("/toUpDataTicket")
    public String toUpDataTicket(Model map,int ticketId){
        //根据id查询数据
        Ticket ticket = ticketService.upDataTicket(ticketId);
        map.addAttribute("ticket",ticket);
        return "ticket-UpData";
    }

    //修改票
    @RequestMapping("/upDataTicket")
    @ResponseBody
    public HashMap<String,Object> upDataTicket(Ticket ticket,HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<String,Object>();
        ticketService.addDataTicket(ticket);
        map.put("info","修改成功");
        return map;
    }



}
